'use strict';

const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;

const db_name_post = 'star-community-post';
const db_name_user = 'star-community-user';
const db_name_like = 'star-community-like';
const db_post = db.collection(db_name_post);

exports.main = async (event, context) => {
	const flag = event.flag;

	if (flag === 0) { // id查询帖子
		const {
			my_user_id,
			id,
		} = event.data;
		return db_post.aggregate()
			.match({
				id,
			})
			.lookup({
				from: db_name_user,
				let: {
					user_id: '$user_id',
				},
				pipeline: $.pipeline()
					.match(dbCmd.expr($.eq(['$id', '$$user_id'])))
					.project({
						_id: 0,
						avatar: 1,
						nickname: 1,
					})
					.done(),
				as: 'userList',
			})
			.lookup({
				from: db_name_like,
				let: {
					id: '$id',
				},
				pipeline: $.pipeline()
					.match(
						dbCmd.expr($.and([
							$.eq(['$enmu_id', '$$id']),
							$.eq(['$user_id', my_user_id]),
						]))
					)
					.project({
						id: 1,
					})
					.done(),
				as: 'likeList',
			})
			.project({
				_id: 0,
				status: 1,
				nickname: $.arrayElemAt(['$userList.nickname', 0]),
				avatar: $.arrayElemAt(['$userList.avatar', 0]),
				commentCount: 1,
				title: 1,
				content: 1,
				createTime: 1,
				updateTime: 1,
				id: 1,
				imgList: 1,
				likeCount: 1,
				shareCount: 1,
				user_id: 1,
				viewCount: 1,
				isLiked: $.cond({
					if: {
						$gt: [dbCmd.size('$likeList'), 0],
					},
					then: true,
					else: false,
				}),
			})
			.end();
	} else if (flag === 1) { // 添加帖子
		const {
			status,
			commentCount,
			title,
			content,
			createTime,
			updateTime,
			id, // 建议使用uuid 或 后端添加后返回的id值
			imgList, // 在此之前请将临时路径替换成线上路径
			likeCount,
			shareCount,
			user_id,
			viewCount,
			reportCount
		} = event.data;
		return db_post.add({
			status,
			commentCount,
			title,
			content,
			createTime,
			updateTime,
			id, // 建议使用uuid 或 后端添加后返回的id值
			imgList, // 在此之前请将临时路径替换成线上路径
			likeCount,
			shareCount,
			user_id,
			viewCount,
			reportCount
		});
	} else if (flag === 2) { // 更新帖子
		const {
			id,
			updateData,
		} = event.data;
		return db_post.where({
			id
		}).update({
			...updateData,
		});
	} else if (flag === 3) { // 查询指定状态的贴子
		const {
			my_user_id,
			status,
			sort,
			skip,
			limit,
		} = event.data;
		return db_post.aggregate()
			.match({
				status, // 0 审核中 1 正常 2 审核不通过 3 已删除 4 已违规
			})
			.sort(sort)
			.skip(skip)
			.limit(limit)
			.lookup({
				from: db_name_user,
				let: {
					user_id: '$user_id',
				},
				pipeline: $.pipeline()
					.match(dbCmd.expr($.eq(['$id', '$$user_id'])))
					.project({
						_id: 0,
						id: 1,
						avatar: 1,
						nickname: 1,
					})
					.done(),
				as: 'userList',
			})
			.lookup({
				from: db_name_like,
				let: {
					id: '$id',
				},
				pipeline: $.pipeline()
					.match(
						dbCmd.expr($.and([
							$.eq(['$enmu_id', '$$id']),
							$.eq(['$user_id', my_user_id]),
						]))
					)
					.project({
						id: 1,
					})
					.done(),
				as: 'likeList',
			})
			.project({
				_id: 0,
				status: 1,
				nickname: $.arrayElemAt(['$userList.nickname', 0]),
				avatar: $.arrayElemAt(['$userList.avatar', 0]),
				commentCount: 1,
				title: 1,
				content: 1,
				createTime: 1,
				updateTime: 1,
				id: 1,
				imgList: 1,
				likeCount: 1,
				shareCount: 1,
				user_id: 1,
				viewCount: 1,
				isLiked: $.cond({
					if: {
						$gt: [dbCmd.size('$likeList'), 0],
					},
					then: true,
					else: false,
				}),
			})
			.end();
	} else if (flag === 4) { // 查询个人主页的帖子
		const {
			user_id,
			statusList,
			sort,
			skip,
			limit,
		} = event.data;
		return db_post.aggregate()
			.match({
				$expr: {
					$and: [{
							$eq: ['$user_id', user_id],
						},
						{
							$in: ['$status', statusList],
						},
					],
				},
			})
			.sort(sort)
			.skip(skip)
			.limit(limit)
			.lookup({
				from: db_name_user,
				let: {
					user_id: '$user_id',
				},
				pipeline: $.pipeline()
					.match(dbCmd.expr($.eq(['$id', '$$user_id'])))
					.project({
						_id: 0,
						id: 1,
						avatar: 1,
						nickname: 1,
					})
					.done(),
				as: 'userList',
			})
			.project({
				_id: 0,
				status: 1,
				nickname: $.arrayElemAt(['$userList.nickname', 0]),
				avatar: $.arrayElemAt(['$userList.avatar', 0]),
				commentCount: 1,
				title: 1,
				content: 1,
				createTime: 1,
				updateTime: 1,
				id: 1,
				imgList: 1,
				likeCount: 1,
				shareCount: 1,
				user_id: 1,
				viewCount: 1,
			})
			.end();
	}
};